<template>
	<view class="normal-login-container">
		<view class="logo-content align-center justify-center">
			<image class="title" src="../static/images/font/Welcome back  EyeRevive2.png" mode="aspectFit"></image>
		</view>
		<view class="login-form-content">
			<view class="input-item flex align-center">
				<image class="my-icon my-icon-user" src="../static/images/icon/user.png" mode="aspectFit"></image>
				<input v-model="loginForm.username" type="text" :placeholder-style="'color:'+green" class="input"
					placeholder="账号" maxlength="30" />
			</view>
			<view class="input-item flex align-center">
				<image class="my-icon my-icon-user" src="../static/images/icon/Vector.png" mode="aspectFit"></image>
				<input v-model="loginForm.password" type="password" :placeholder-style="'color:'+green" class="input"
					placeholder="密码" maxlength="20" />
			</view>
			<view class="login-extra flex">
				<view class="flex" @tap="rememberPwd = !rememberPwd">
					<text :class="'icon-rememberPwd ' + (rememberPwd ? 'cuIcon-roundcheckfill' : 'cuIcon-roundcheck')"></text>
					<text class="remember-password">记住密码</text>
				</view>
				<text class="forget-password" @tap="register">注册账号</text>
			</view>
			<view class="action-btn">
				<button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				green: "#00E98E",
				rememberPwd: false,
				// 用户注册开关
				globalConfig: getApp().globalData.config,
				loginForm: {
					username: "",
					password: "",
				}
			}
		},
		created() {
			// this.getCode()
		},
		onLoad() {
			const savedUsername = uni.getStorageSync('username');
			const savedPassword = uni.getStorageSync('password');
			if (savedUsername && savedPassword) {
				this.loginForm.username = savedUsername;
				this.loginForm.password = savedPassword;
				this.rememberPwd = true;
			}
		},
		methods: {
			// 注册账号
			register() {
				this.$tab.navigateTo(`/pages/register`)
			},
			// 登录方法
			async handleLogin() {
				if (this.loginForm.username === "") {
					this.$modal.msgError("请输入账号")
				} else if (this.loginForm.password === "") {
					this.$modal.msgError("请输入密码")
				} else {
					this.$modal.loading("登录中，请耐心等待...")
					this.pwdLogin()
				}
			},
			// 密码登录
			async pwdLogin() {
				this.$store.dispatch('Login', this.loginForm).then(() => {
					// 成功
					this.$modal.closeLoading()
					this.loginSuccess()
				}).catch((err) => {
					// 失败
					this.$modal.closeLoading()
					this.$modal.msgError(err)
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 保存账号密码
				if (this.rememberPwd) {
					uni.setStorageSync('username', this.loginForm.username);
					uni.setStorageSync('password', this.loginForm.password);
				} else {
					// 如果记住密码未被选中，清除本地存储中的用户名和密码
					uni.removeStorageSync('username');
					uni.removeStorageSync('password');
				}
				// 设置用户信息
				this.$store.dispatch('GetInfo', this.loginForm).then(res => {
					this.$tab.reLaunch('/pages/index')
				})
			},
		}
	}
</script>

<style lang="scss">
	$green: #00E98E;

	page {
		// background-color: #ffffff;
		background-image: url("../static/images/background/1.login.jpg");
		background-size: cover;
		padding-top: 37.2vh;
		height: 100vh;
	}

	.normal-login-container {
		width: 100%;
		font-weight: bold;
		color: #555;

		.logo-content {
			width: 100%;
			text-align: center;
			position: relative;

			.title {
				width: 62%;
				height: 150upx;
				left: 8upx;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 20upx auto;
			margin-top: 5.5vh;
			width: 76%;

			.input-item {
				margin: 1.6vh auto;
				background-color: #ffffffd9;
				// height: 125upx;
				height: calc(45upx + 4.1vh);
				border-radius: 18upx;
				// border: 4upx solid #40eb94d9;
				border: 4upx solid $green;

				.my-icon {
					width: 32upx;
					height: 32upx;
					margin-left: 25upx;
					font-size: 32upx;
					margin-right: 8upx;
					font-weight: initial;
				}

				.input {
					width: 100%;
					font-size: 26upx;
					color: #4cd399;
					text-align: left;
					padding-left: 32upx;
				}

			}

			.login-extra {
				height: 30upx;
				font-size: 23upx;
				color: #50504F;
				justify-content: space-between;
				align-items: center;

				.icon-rememberPwd {
					font-size: 32upx;
					margin-right: 15upx;
					margin-top: -3upx;
					font-weight: initial;
				}

				.cuIcon-roundcheck {
					color: #aaa;
				}

				.cuIcon-roundcheckfill {
					color: $green;
				}

				// .my-icon-checkmark {
				// 	width: 30upx;
				// 	height: 30upx;
				// 	margin: 2upx 20upx 0 10upx;
				// 	background-image: url("../static/images/icon/checkmark.png");
				// 	background-size: 30upx;

				// 	&.active {
				// 		background-image: url("../static/images/icon/checkmark2.png");
				// 	}
				// }
			}

			.action-btn {
				margin-top: 12.1vh;

				.login-btn {
					font-size: 28upx;
					font-weight: initial;
					background: #03D482;
					height: 90upx;
					height: calc(40upx + 3vh);
				}
			}

			.reg {
				margin-top: 15px;
			}

			// .xieyi {
			// 	color: #333;
			// 	margin-top: 20px;
			// }

			// .login-code {
			// 	height: 38px;
			// 	float: right;

			// 	.login-code-img {
			// 		height: 38px;
			// 		position: absolute;
			// 		margin-left: 10px;
			// 		width: 200upx;
			// 	}
			// }
		}
	}
</style>